package com.ai.bitcoin.controller;

import com.ai.bitcoin.constant.Errors;
import com.ai.bitcoin.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.async.DeferredResult;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

/**
 * @author Jason
 * @date 2018/2/27
 */
@Slf4j
public abstract class CommonController {

    public static String getRandom() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String getRandom(int len) {
        if (len > 32 || len < 1)
            return null;
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, len);
    }

    public void checkParams(String... strings) throws BusinessException {
        for (int i = 0; i < strings.length; i++) {
            if (StringUtils.isEmpty(strings[i])) {
                throw new BusinessException(Errors.PARAMS_ERROR, Errors.PARAMS_ERROR_INFO);
            }
        }
    }


    public DeferredResult deferredContent(DeferredResult<String> deferredResult, String errCode, String errInfo) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errCode", errCode);
        jsonObject.put("errInfo", errInfo);
        log.debug("springMvc 响应:{}", jsonObject);
        deferredResult.setResult(jsonObject.toString());
        return deferredResult;
    }

    public DeferredResult deferredContent(DeferredResult<String> deferredResult, String responseMsg) {
        log.debug("springMvc 响应:{}", responseMsg);
        deferredResult.setResult(responseMsg);
        return deferredResult;
    }

    public DeferredResult deferredContent(DeferredResult<String> deferredResult, BusinessException e) {
        log.debug("springMvc 响应:{}", e.getMessage());
        return deferredContent(deferredResult, e.getErrCode(), e.getErrInfo());
    }

    public DeferredResult deferredContent(DeferredResult<String> deferredResult, JSONObject responseMsg) {
        return deferredContent(deferredResult, responseMsg.toString());
    }

    public JSONObject readJson(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        Map pmap = request.getParameterMap();
        Iterator it = pmap.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next().toString();
            String[] values = (String[]) pmap.get(key);
            jsonObject.put(key, values[0]);
        }
        return jsonObject;
    }

    public void printReq(JSONObject jsonObject) {
        if (jsonObject == null)
            return;
        JSONObject newJ = new JSONObject();
        Iterator iterator = jsonObject.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
            if (!StringUtils.equals(entry.getKey(), "pic")) {
                newJ.put(entry.getKey(), entry.getValue());//newJ.put((String) entry.getKey(), (String) entry.getValue());
            }
        }
        log.info("request: {}", newJ.toString());
    }

    public String getDicString(JSONObject jsonObject) {
        TreeMap<String, String> treeMap = new TreeMap<String, String>(jsonObject);
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            if (entry.getKey() != null) {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        return sb.substring(0, sb.length() - 1);
    }
}
